package com.mixsmart.core.service.impl;

import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mixsmart.core.IMixSmart;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.core.bean.Paginate;
import com.mixsmart.core.helper.ObjectHelper;
import com.mixsmart.core.search.filter.SearchFilter;
import com.mixsmart.core.search.filter.SearchFilterFactory;
import com.mixsmart.core.service.IQueryService;
import com.mixsmart.core.service.IResQueryService;
import com.mixsmart.core.xml.sqlmap.SqlMapUtils;
import com.mixsmart.core.xml.sqlmap.bean.SqlUnit;
import com.mixsmart.utils.CollectionUtils;
import com.mixsmart.utils.StringUtils;

/**
 * 资源查询服务实现类
 * @author lmq
 * @version 1.0
 * @since JDK版本大于等于1.6
 * 2016年1月15日
 */
@Service
public class ResQueryService implements IResQueryService {

	private static final Logger logger = Logger.getLogger(ResQueryService.class);
	
	@Autowired
	private IQueryService queryServ;
	
	@Override
	public MixResponse<Object> getDatas(String resName,HashMap<String, Object> params) {
		MixResponse<Object> mixResp = new MixResponse<Object>();
		mixResp.setMsg(IMixSmart.OP_NOT_DATA_SUCCESS_MSG);
		logger.info("正在查询资源名称为:["+resName+"]...");
		if(StringUtils.isNotEmpty(resName)) {
			SqlUnit sqlUnit = SqlMapUtils.getSql(resName);
			if(null != sqlUnit) {
				List<Object> lists = null;
				try {
					if(sqlUnit.isFilter()) {
						lists = queryServ.queryObjSqlFilter(sqlUnit.getSql(), params);
					} else {
						lists = queryServ.queryObjSql(sqlUnit.getSql(), params);
					}
					if(CollectionUtils.isNotEmpty(lists)) {
						mixResp.setResult(IMixSmart.OP_SUCCESS);
						mixResp.setDatas(ObjectHelper.handleObjDate(lists));
						mixResp.setMsg(IMixSmart.OP_SUCCESS_MSG);
						mixResp.setTotalNum(lists.size());
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					lists = null;
					sqlUnit = null;
				}
			} else {
				logger.error("资源名称["+resName+"]，未找到对应的SQL语句");
			}
		} else {
			logger.error("资源名称为空");
		}
		return mixResp; 
	}

	@Override
	public <E> MixResponse<E> getDatas(String resName, HashMap<String, Object> params, Class<?> clazz) {
		MixResponse<E> mixResp = new MixResponse<E>();
		mixResp.setMsg(IMixSmart.OP_NOT_DATA_SUCCESS_MSG);
		if(StringUtils.isNotEmpty(resName)) {
			SqlUnit sqlUnit = SqlMapUtils.getSql(resName);
			if(null != sqlUnit) {
				List<E> lists = null;
				try {
					if(sqlUnit.isFilter()) {
						lists = queryServ.querySqlFilter(sqlUnit.getSql(), params, clazz);
					} else {
						lists = queryServ.querySql(sqlUnit.getSql(), params, clazz);
					}
					if(CollectionUtils.isNotEmpty(lists)) {
						mixResp.setResult(IMixSmart.OP_SUCCESS);
						mixResp.setDatas(lists);
						mixResp.setMsg(IMixSmart.OP_SUCCESS_MSG);
						mixResp.setTotalNum(lists.size());
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					lists = null;
					sqlUnit = null;
				}
			} else {
				logger.error("资源名称["+resName+"]，未找到对应的SQL语句");
			}
		} else {
			logger.error("资源名称为空");
		}
		return mixResp; 
	}

	@Override
	public MixResponse<Object> getDatas(String resName, HashMap<String, Object> params, 
			int pageNo, int pageSize) {
		MixResponse<Object> mixResp = new MixResponse<Object>();
		mixResp.setMsg(IMixSmart.OP_NOT_DATA_SUCCESS_MSG);
		if(StringUtils.isNotEmpty(resName)) {
			SqlUnit sqlUnit = SqlMapUtils.getSql(resName);
			if(null != sqlUnit) {
				Paginate<Object> page = null;
				try {
					if(sqlUnit.isFilter()) {
						page = queryServ.queryObjSqlFilter(sqlUnit.getSql(), params, pageNo, pageSize);
					} else {
						page = queryServ.queryObjSql(sqlUnit.getSql(), params, pageNo, pageSize);
					}
					if(null != page && CollectionUtils.isNotEmpty(page.getDatas())) {
						mixResp.setResult(IMixSmart.OP_SUCCESS);
						mixResp.setDatas(ObjectHelper.handleObjDate(page.getDatas()));
						mixResp.setMsg(IMixSmart.OP_SUCCESS_MSG);
						mixResp.setTotalNum(page.getTotalCount());
						mixResp.setTotalPage(page.getTotalPages());
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					page = null;
					sqlUnit = null;
				}
			} else {
				logger.error("资源名称["+resName+"]，未找到对应的SQL语句");
			}
		} else {
			logger.error("资源名称为空");
		}
		return mixResp; 
	}

	@Override
	public <E> MixResponse<E> getDatas(String resName, HashMap<String, Object> params, 
			int pageNo, int pageSize, Class<?> clazz) {
		MixResponse<E> mixResp = new MixResponse<E>();
		mixResp.setMsg(IMixSmart.OP_NOT_DATA_SUCCESS_MSG);
		if(StringUtils.isNotEmpty(resName)) {
			SqlUnit sqlUnit = SqlMapUtils.getSql(resName);
			if(null != sqlUnit) {
				Paginate<E> page = null;
				try {
					if(sqlUnit.isFilter()) {
						page = queryServ.querySqlFilter(sqlUnit.getSql(), params, pageNo, pageSize, clazz);
					} else {
						page = queryServ.querySql(sqlUnit.getSql(), params, pageNo, pageSize, clazz);
					}
					if(null != page && CollectionUtils.isNotEmpty(page.getDatas())) {
						mixResp.setResult(IMixSmart.OP_SUCCESS);
						mixResp.setDatas(page.getDatas());
						mixResp.setMsg(IMixSmart.OP_SUCCESS_MSG);
						mixResp.setTotalNum(page.getTotalCount());
						mixResp.setTotalPage(page.getTotalPages());
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					page = null;
					sqlUnit = null;
				}
			} else {
				logger.error("资源名称["+resName+"]，未找到对应的SQL语句");
			}
		} else {
			logger.error("资源名称为空");
		}
		return mixResp; 
	}

	@Override
	public MixResponse<Object> getDatas(String resName, SearchFilter searchFilter) {
		HashMap<String,Object> params = null;
		if(null != searchFilter) {
			logger.debug("搜索对象["+searchFilter.toString()+"]转化为HashMap...");
			params = SearchFilterFactory.filterBeanToMap(searchFilter);
		}
		return this.getDatas(resName, params);
	}

	@Override
	public MixResponse<Object> getDatas(String resName, SearchFilter searchFilter, int pageNo, int pageSize) {
		HashMap<String,Object> params = null;
		if(null != searchFilter) {
			logger.debug("搜索对象["+searchFilter.toString()+"]转化为HashMap...");
			params = SearchFilterFactory.filterBeanToMap(searchFilter);
		}
		return this.getDatas(resName, params, pageNo, pageSize);
	}

	@Override
	public MixResponse<Long> count(String resName, HashMap<String, Object> params) {
		MixResponse<Long> mixResp = new MixResponse<Long>();
		mixResp.setMsg(IMixSmart.OP_NOT_DATA_SUCCESS_MSG);
		if(StringUtils.isNotEmpty(resName)) {
			SqlUnit sqlUnit = SqlMapUtils.getSql(resName);
			if(null != sqlUnit) {
				Long count = -1l;
				try {
					if(sqlUnit.isFilter()) {
						count = queryServ.countSqlFilter(sqlUnit.getSql(), params);
					} else {
						count = queryServ.countSql(sqlUnit.getSql(), params);
					}
					if(count>-1) {
						mixResp.setResult(IMixSmart.OP_SUCCESS);
						mixResp.setData(count);
						mixResp.setMsg(IMixSmart.OP_SUCCESS_MSG);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					sqlUnit = null;
				}
			} else {
				logger.error("资源名称["+resName+"]，未找到对应的SQL语句");
			}
		} else {
			logger.error("资源名称为空");
		}
		return mixResp; 
	}

}
